<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频名称" prop="videoName">
        <el-input v-model="queryParams.videoName" placeholder="请输入视频名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['kenuo:video:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['kenuo:video:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['kenuo:video:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="视频名称" align="center" prop="videoName" />
      <el-table-column label="视频类型" align="center" prop="vtype">
        <!-- <template slot-scope="scope">
          <el-select v-model="scope.row.vtype" placeholder="" >
            <el-option :label="item.dictLabel" :value="item.dictValue"  v-for="(item,index) in tagsList" :key=index></el-option>
          </el-select>
        </template> -->



         <template slot-scope="scope">
          <!-- {{ scope.row.vtype == 1 ? "未绑定" : "已绑定" }} -->

          <div v-show="scope.row.vtype == 1">盆底肌修复</div>
          <div v-show="scope.row.vtype == 2">产后恢复</div>
          <div v-show="scope.row.vtype == 3">多媒体生物反馈</div>
          <div v-show="scope.row.vtype == 4">训练</div>

        </template>



      </el-table-column>
      <!-- 1:盆底肌修复  2:产后恢复  3:多媒体生物反馈 4:训练 -->
      <el-table-column label="视频封面" align="center" prop="cover">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" v-if="scope.row.cover" :src="scope.row.cover" fit=""></el-image>
          <div v-else>暂无</div>
        </template>
      </el-table-column>
      <el-table-column label="相关说明" align="center" prop="des" />
      <el-table-column label="视频状态" align="center" prop="state">
        <template slot-scope="scope">
          {{scope.row.state == "1" ? "上架" : scope.row.state == "2" ? "下架" : "无"}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="备用字段1" align="center" prop="remarkOne" />
      <el-table-column label="备用字段2" align="center" prop="remarkTwo" />
      <el-table-column label="备用字段3" align="center" prop="remarkThree" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['kenuo:video:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['kenuo:video:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="checkVideo(scope.row)">查看</el-button>
          <!-- 1上架  2下架 -->
          <el-button size="mini" type="text" :icon="scope.row.state == 2 ? 'el-icon-top' : 'el-icon-bottom'"
            @click="isWork(scope.row)">{{scope.row.state == 2 ? '上架' : '下架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="form.videoName" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item label="视频类型" prop="vtype">
          <!-- 1:盆底肌修复  2:产后恢复  3:多媒体生物反馈 4:训练 -->
          <el-select v-model="form.vtype" placeholder="">
            <el-option :label="item.dictLabel" :value="item.dictValue" v-for="(item,index) in tagsList" :key="index"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="视频封面" prop="cover">
          <UploadImg v-model="form.cover" @operateImg="operateImg" uploadType="png,jpg,jpeg" :imgUrl="imgUrl">
          </UploadImg>
          <el-link type="danger" disabled>注意：封面图尺寸只能是512 * 300,大小不超过2M</el-link>
        </el-form-item>
        <el-form-item label="上传视频" prop="video">
          <UploadFiles v-model="form.video" :fileList="fileList" @operateFiles="operateFiles" :uploadType="uploadType">
          </UploadFiles>
          <el-link type="danger" disabled>{{des}}</el-link>
        </el-form-item>
        <el-form-item label="视频说明" prop="des">
          <!-- <el-input v-model="form.des" placeholder="请输入视频说明" /> -->
          <el-input type="textarea" :rows="5" v-model="form.des" placeholder="请输入视频说明">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看视频 -->
    <el-dialog title="查看视频" :visible.sync="openVideo" width="50%" height="800" v-if="openVideo" append-to-body>
      <video :src="videoUrl" style="width:100%" controls="controls"></video>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listVideo,
    getVideo,
    delVideo,
    addVideo,
    updateVideo
  } from "@/api/kenuo/video";
  import {
    getType
  } from '@/api/kenuo/common'
  import UploadFiles from '@/views/components/uploadFiles.vue';
  import UploadImg from './uploadImg'
  import UploadVideo from './uploadVideo'

  export default {
    name: "Video",
    components: {
      UploadFiles,
      UploadImg,
      UploadVideo
    },
    data() {
      return {
        tagsList: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        videoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          videoName: null,
          vType: null,
          cover: null,
          video: null,
          des: null,
          state: null,
          remarkOne: null,
          remarkTwo: null,
          remarkThree: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          videoName: [{
            required: true,
            message: "视频名称不能为空",
            trigger: "blur"
          }],
          vtype: [{
            required: true,
            message: "请选择视频类型",
            trigger: "blur"
          }],
          video: [{
            required: true,
            message: "请上传视频",
            trigger: "blur"
          }],
        },
        imgUrl: '',
        fileList: [],
        uploadType: '.mp4',
        des: '文件不大于200M且类型为mp4，(注意：最多上传一个文件)',
        openVideo: false, //查看视频
        videoUrl: '',

      };
    },
    created() {
      this.getVideoType()
      this.getList();

    },
    methods: {
      // 获取视频标签
      getVideoType() {
        getType({
          dictType: "video_tag"
        }).then(response => {
          this.tagsList = response.rows;
        });
      },
      operateFiles(val) {
        this.fileList = val
        if (val.length > 0) {
          this.form.video = val[0].url
        } else {
          this.form.video = ''
        }

      },
      operateImg(val) {
        this.imgUrl = val
        this.form.cover = val
      },
      //查看视频
      checkVideo(row) {

        this.videoUrl = row.video
        this.openVideo = true
      },
      //上下架
      isWork(row) {
        let obj = {
          id: row.id,
          state: row.state == "2" ? "1" : "2"
        }
        updateVideo(obj).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listVideo(this.queryParams).then(response => {
          this.videoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          videoName: null,
          vType: null,
          cover: null,
          video: null,
          des: null,
          state: null,
          createTime: null,
          updateTime: null,
          remarkOne: null,
          remarkTwo: null,
          remarkThree: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        console.log("fg3r5tg35",this.queryParams)
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getVideo(id).then(response => {
          this.form = response.data;
           this.form.vtype = String(this.form.vtype)
          this.imgUrl = this.form.cover
          if (this.form.video != "") {
            this.fileList = [{
              name: 'video.jpg',
              url: response.data.video
            }]
          }
          this.open = true;
          this.title = "修改";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateVideo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addVideo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除该数据项？').then(function() {
          return delVideo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('kenuo/video/export', {
          ...this.queryParams
        }, `video_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
